package com.demo.security.access.intercept;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;

/**
 * 代理过滤器调用资源元数据安全数据类
 * <br>
 * 通过不同来源,为资源安全过滤器提供对资源安全过滤的元数据源
 * 
 * @author ryuu.kk
 *
 */
public class DelegateFilterInvocationSecurityMetadataSource implements FilterInvocationSecurityMetadataSource {

	protected final Log logger = LogFactory.getLog(getClass());

	private FilterInvocationSecurityMetadataSource[] sources;

    //~ Constructors ===================================================================================================

    public DelegateFilterInvocationSecurityMetadataSource(FilterInvocationSecurityMetadataSource... sources) {
    	this.sources = sources;
    }

    //~ Methods ========================================================================================================
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        Set<ConfigAttribute> allAttributes = new HashSet<ConfigAttribute>();

        for (FilterInvocationSecurityMetadataSource source : sources) {
        	allAttributes.addAll(source.getAllConfigAttributes());
        }
        return allAttributes;
    }

    public Collection<ConfigAttribute> getAttributes(Object object) {
        
        for (FilterInvocationSecurityMetadataSource source : sources) {
        	Collection<ConfigAttribute> collections = source.getAttributes(object);
        	if(collections != null) {
        		return collections;
        	}
        }
        return null;
    }

    public boolean supports(Class<?> clazz) {
        return FilterInvocation.class.isAssignableFrom(clazz);
    }
}
